package com.hfut.jpoa.service.config;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.exception.ServiceException;
import com.hfut.jpoa.common.spring.SpringHelper;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.service.SysparaService;

/**
 * @项目名：商品中心
 * @包名：com.tydic.ttp.util
 * @类名：PropertiesReader.java
 * @类描述：加载配置文件类
 * @创建人：zhaochun
 * 
 */
public class SystemConfigUtil
{

	private static Logger logger = LoggerFactory
	        .getLogger(SystemConfigUtil.class);

	/**
	 * getPropertyFromDC 根据key值从数据缓存中读取配置参数单个参数配时参数索引PROPERTY_INDEX必须为0
	 * 
	 * @param property
	 * @return String
	 * @since 1.0.0
	 */
	public static String getPropertyFromDC(String property_key)
	{
		SysparaService SysparaService = (SysparaService) SpringHelper
		        .getSpringHelper().getBean("sysparaService");
		if (!validate(property_key))
		{
			return null;
		}
		List<Syspara> all = null;
		try
		{
			all = SysparaService.getSyspara(property_key);
		} catch (ServiceException e)
		{
			logger.error("查询日志 异常", e);
		}
		if (all != null && all.size() > 0)
		{
			return all.get(0).getValue();
		}
		return null;
	}

	public static String getPropertyFromDCGetName(String property_key)
	{
		SysparaService SysparaService = (SysparaService) SpringHelper
		        .getSpringHelper().getBean("SysparaService");
		if (!validate(property_key))
		{
			return null;
		}
		List<Syspara> all = null;
		try
		{
			all = SysparaService.getSyspara(property_key);
		} catch (ServiceException e)
		{
			logger.error("查询日志 异常", e);
		}
		if (all != null && all.size() > 0)
		{
			return all.get(0).getName();
		}
		return null;
	}

	/**
	 * getPropertyArrayFromDC 根据type值取出多个配置参数对象
	 * 
	 * @param property_key
	 * @return List<String>
	 * @since 1.0.0
	 */
	public static List<Syspara> getSysparaArrayFromDC(String property_type)
	{
		if (!validate(property_type))
		{
			return null;
		}
		SysparaService SysparaService = (SysparaService) SpringHelper
		        .getSpringHelper().getBean("SysparaService");
		List<Syspara> all = null;
		Syspara config = new Syspara();
		config.setType(property_type);
		 try
		 {
		 all = SysparaService.querySysparaList(config);
		all = new ArrayList<Syspara>();
		} catch (ServiceException e)
		{
			logger.error("查询日志 异常", e);
		}
		return all;
	}

	private static boolean validate(String property_key)
	{
		if (property_key == null)
		{
			logger.info("property_key is null");
			return false;
		}
		return true;
	}

}
